import json

import requests


def search(text):
    url = "https://v1.hitokoto.cn/nm/search/" + text + "?type=SO"
    content = requests.get(url)
    ret = json.loads(content.text)

    i = 0
    b = 0

    lists = []
    try:
        while b <= 30:
            # print(a['name'])
            singer_id = ret['result']['songs'][i]['artists'][0]['id']  # 歌手的id
            album_name = ret['result']['songs'][i]['album']['name']  # 专辑名字
            album_id = ret['result']['songs'][i]['album']['id']  # 专辑id
            song_name = ret['result']['songs'][i]['name']  # 单曲名字
            mv_id = ret['result']['songs'][i]['mvid']  # mv_id
            song_id = ret['result']['songs'][i]['id']  # 单曲id
            author_name = ret['result']['songs'][i]['artists'][0]['name']  # 歌手名字
            author = ret['result']['songs'][0]['artists'][0]['name']  # 第一个歌手
            album = ret['result']['songs'][0]['album']['name']  # 第一个专辑
            singer = ret['result']['songs'][0]['artists'][0]['id']  # 第一个歌手id
            i += 1
            b += 1
            dicts = {
                'singer_id': singer_id,
                'album_name': album_name,
                'song_name': song_name,
                'song_id': song_id,
                'album_id': album_id,
                'author_name': author_name,
                'b': b,
                'mv_id': mv_id,
                'singer': singer,
                'author': author,
                'album': album,
            }
            lists.append(dicts)
    except Exception as e:
        print(e)

    return lists

def user_detail(singer):
    # 获取用户详情信息
    author_url = 'https://v1.hitokoto.cn/nm/artist/' + str(singer)
    content = requests.get(author_url)
    rets = json.loads(content.text)
    singer_detail_list = []
    name = rets['artist']['name']  # 歌手名
    pic = rets['artist']['picUrl']  # 歌手图片
    desc = rets['artist']['briefDesc']  # 歌手简介
    music_num = rets['artist']['musicSize']  # 单曲个数
    album_num = rets['artist']['albumSize']  # 专辑个数
    mv = rets['artist']['mvSize']  # MV个数
    deta={
        'name': name,
        'pic': pic,
        'desc': desc,
        'music_num': music_num,
        'album_num': album_num,
        'mv': mv,
    }
    singer_detail_list.append(deta)
    return singer_detail_list